﻿using System;
using System.Collections;
using System.Collections.Generic;
using WzhAddon.ExtMP3;
using WzhAddon.Utils;
using Studio;
using UnityEngine;

namespace WzhAddon
{
	public class HSVMDAnimationMgr : BaseMgr<HSVMDAnimationMgr>
	{
		public enum KEY { VMD_PLAY_ALL, VMD_STOP_ALL, VMD_PAUSE_ALL }
		private Dictionary<KEY, string> DEFAULT_KEYS = new Dictionary<KEY, string> { { KEY.VMD_PLAY_ALL, "Alt+Y" }, { KEY.VMD_STOP_ALL, "Alt+T" }, { KEY.VMD_PAUSE_ALL, "Alt+U" } };
		private Dictionary<KEY, KeyUtil> keyUtils = new Dictionary<KEY, KeyUtil>();
		public List<HSVMDAnimationController> controllers = new List<HSVMDAnimationController>();
	
		public override void Init()
		{
			this.InitKey();
		}

		private void InitKey()
		{
			this.keyUtils = new Dictionary<KEY, KeyUtil>();
			//foreach (KEY key in this.DEFAULT_KEYS.Keys)
			//{
   //             KeyUtil keyUtil = KeyUtil.Parse(Settings.Instance.ShortcutKey(key.ToString(), this.DEFAULT_KEYS[key]));
   //             if (keyUtil == null) keyUtil = KeyUtil.Parse(this.DEFAULT_KEYS[key]);
			//	this.keyUtils[key] = keyUtil;
			//}
		}

		public bool AddController()
		{
			if (Singleton<Studio.Studio>.Instance == null) return false;
			ObjectCtrlInfo studioChara = StudioUtils.GetCurrentChar();
			OCIChar oCIChar = studioChara as OCIChar;
			if (oCIChar == null) return false;

			ChaControl chaControl = oCIChar.charInfo;
			if (chaControl == null) return false;

			HSVMDAnimationController hsvmdanimationController = chaControl.gameObject.GetComponent<HSVMDAnimationController>();
			if (hsvmdanimationController == null)
			{
				hsvmdanimationController = chaControl.gameObject.AddComponent<HSVMDAnimationController>();
				hsvmdanimationController.Init(oCIChar, chaControl);
				controllers.Add(hsvmdanimationController);
			}
			return true;
		}

		public void LoadVMDAnimation(List<string> charAniPathList, string cameraAniPath, string map3Path)
		{
			if (charAniPathList.Count == 0) return;
			if (controllers.Count == 0) return;
			// 角色动画加载
			HSVMDAnimationController tpCol = null;
			for (int i = 0; i < controllers.Count; i++)
            {
				tpCol = controllers[i];
				if (i < charAniPathList.Count)
				{
					tpCol.gameObject.SetActive(true);
					tpCol.LoadVMDAnimation(charAniPathList[i]);
				}
				else
				{
					tpCol.gameObject.SetActive(false);
				}
			}

			MMD_VmdCameraLoad CameraVmd = MMD_VmdCameraLoad.Instance;
			CameraVmd.SetEnnabled(string.IsNullOrEmpty(cameraAniPath) == false);
			CameraVmd.LoadVmd(cameraAniPath, controllers[0]);

			if (string.IsNullOrEmpty(map3Path)) MP3Player.Stop();
			else MP3Player.Open(map3Path);

			PlayAll();
		}
		public void PlayAll()
		{
			if (controllers.Count == 0) return;
			foreach (HSVMDAnimationController hsvmdanimationController in this.controllers)
			{
				hsvmdanimationController.Play();
			}
			MP3Player.Play(this.controllers[0]);
		}
		public void StopAll()
		{
			foreach (HSVMDAnimationController hsvmdanimationController in this.controllers)
			{
				hsvmdanimationController.Stop();
			}
			MP3Player.Stop();
		}
		public void PauseAll()
		{
			foreach (HSVMDAnimationController hsvmdanimationController in this.controllers)
			{
				hsvmdanimationController.Pause();
			}
			MP3Player.Pause();
		}
		public void FileLoadAll(string fileName, string lipFile = null)
		{
			foreach (HSVMDAnimationController hsvmdanimationController in this.controllers)
			{
				hsvmdanimationController.LoadVMDAnimation(fileName, lipFile);
			}
		}

		public void LoadAudio(string path, int type = 1)
		{
			base.StartCoroutine(this.LoadAudioEnum(path, type));
		}
		private IEnumerator LoadAudioEnum(string path, int type)
		{
			Console.WriteLine("Start load: " + path);
			if (type == 2)
			{
                //using (UnityWebRequest uwr = UnityWebRequest.GetAudioClip("file://" + path, 13))
                //{
                //    yield return uwr.Send();
                //    if (uwr.isError)
                //    {
                //        Debug.LogError(uwr.error);
                //        yield break;
                //    }
                //    MP3Player.audioSrc.clip = DownloadHandlerAudioClip.GetContent(uwr);
                //    Console.WriteLine("Load end 1: ");
                //    Console.WriteLine(MP3Player.audioSrc.clip);
                //}
                //UnityWebRequest uwr = null;
            }
			else
			{
				WWW www = new WWW("file://" + path);
				yield return www;
				if (www != null && string.IsNullOrEmpty(www.error))
				{
					Console.WriteLine("Load end: ");
					Console.WriteLine(www.audioClip);
					MP3Player.audioSrc.clip = www.audioClip;
				}
				www = null;
			}
			yield break;
			//yield break;
		}

		private void Update()
		{
			if (this.controllers == null) return;
			if (this.controllers.Count == 0) return;

			//if (this.GetKeyDown(KEY.VMD_PLAY_ALL))
			//{
			//	this.PlayAll();
			//	MP3Player.Play(this.controllers[0]);
			//}
			//else if (this.GetKeyDown(KEY.VMD_STOP_ALL))
			//{
			//	this.StopAll();
			//	MP3Player.Stop();
			//}
			//else if (this.GetKeyDown(KEY.VMD_PAUSE_ALL))
			//{
			//	this.PauseAll();
			//	MP3Player.Pause();
			//}

			MP3Player.Update();
		}

		private bool GetKeyDown(KEY key)
		{
			return this.keyUtils.ContainsKey(key) && this.keyUtils[key].TestKeyDown();
		}



	}
}
